package day04.pm;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/**
 * 窗体类
 */
public class Demo01 {
    public static void main(String[] args) throws Exception {
        JFrame jFrame = new JFrame("捕鱼达人");
        jFrame.setSize(800,480);
        jFrame.setLocationRelativeTo(null);
        jFrame.setResizable(false);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Pool pool = new Pool();
        jFrame.add(pool);

        jFrame.setVisible(true);
        //调用游戏开始的方法action
        pool.action();
    }
}//Demo01结束

//Pool开始
class Pool extends JPanel {
    BufferedImage bufferedImage;//背景图
    Fish[] fish;//鱼数组
    Net net;//渔网
    int count;//捕到的鱼的数量
    int score;//捕鱼获得的分数
    /**构造方法*/
    public Pool() throws IOException {
        bufferedImage = ImageIO.read(
                new File("fish/images/bg.jpg"));
        net = new Net();
        fish = new Fish[9+9+9+4];
        for(int i=0;i<9;i++){
            fish[i] = new Fish("fish0"+(i+1));
            fish[i+9] = new Fish("fish0"+(i+1));
            fish[i+9+9] = new Fish("fish0"+(i+1));
        }
        fish[fish.length-4] = new Fish("fish13");
        fish[fish.length-3] = new Fish("fish14");
        fish[fish.length-2] = new Fish("fish13");
        fish[fish.length-1] = new Fish("fish14");
    }

    /**绘图方法*/
    @Override
    public void paint(Graphics g) {
        g.drawImage(bufferedImage,0,0,null);
        for(int i=0;i<fish.length;i++){
            g.drawImage(fish[i].image,fish[i].x,fish[i].y,null);
        }
        if(net.show){//渔网处于显示状态就把他画出来
            //计算x1,y1坐标
            int x1 = net.x - net.width/2;
            int y1 = net.y - net.height/2;
            g.drawImage(net.image,x1,y1,null);
        }
        //绘制数量和分数
        g.setColor(Color.WHITE);
        Font f = new Font("宋体",Font.BOLD,15);
        g.setFont(f);
        g.drawString("捕鱼数量:"+count+"条",20,30);
        g.drawString("捕鱼得分:"+score+"分",20,60);

    }

    /**在Pool类中增加捕鱼的方法*/
    public void catchFish(){
        for(int i=fish.length-1;i>=0;i--){
            //判断鱼是否被抓到
            //如果被抓到，
            if(net.catchThe(fish[i])){
                //计数
                count++;
                score += fish[i].width/10;
                fish[i].catched = true;
                break;//抓到后循环退出
            }

            //如果没被抓到，啥也不干
        }
    }
    /**开始的方法*/
    public void action() throws InterruptedException {
        MouseAdapter ma = new MouseAdapter() {
            @Override//鼠标按下
            public void mousePressed(MouseEvent e) {
                catchFish();
                System.out.println("----鼠标按下了----------");
            }

            @Override//鼠标进入
            public void mouseEntered(MouseEvent e) {
                net.show = true;
                System.out.println("----鼠标进入----------");
            }

            @Override//鼠标移出
            public void mouseExited(MouseEvent e) {
                net.show = false;
//                System.out.println("----鼠标移出去了----------");
            }

            @Override //鼠标移动
            public void mouseMoved(MouseEvent e) {
                int x = e.getX();
                int y = e.getY();
                net.moveTo(x,y);
                System.out.println("----["+x+","+y+"]----");
//                System.out.println("----鼠标在移动----------");
            }
        };
        //添加监听事件
        this.addMouseListener(ma);
        this.addMouseMotionListener(ma);

        //启动线程
        for(int i=0;i<fish.length;i++){
            fish[i].start();
        }
        while(true){//重画
            Thread.sleep(20);
            repaint();
        }
    }
}//Pool结束

//Fish开始
class Fish extends Thread {//鱼自己游动
    BufferedImage[] images;//鱼的10张图片数组
    BufferedImage image;//鱼的当前图片
    int width;//图片宽
    int height;//图片高
    int x;
    int y;
    int speed;//速度
    int index;//计数用
    boolean catched;//鱼是否被抓
    BufferedImage catch01;
    BufferedImage catch02;

    public Fish(String name) throws IOException {
        images = new BufferedImage[10];
        for(int i=0;i<images.length;i++){
            String path = "fish/images/"+name+"_0"+i+".png";
            images[i] = ImageIO.read(new File(path));
        }
        image = images[0];
        width = image.getWidth();
        height = image.getHeight();
        Random r = new Random();
        x = r.nextInt(800-width);
        y = r.nextInt(480-height);
        speed = r.nextInt(5)+1;
        catched = false;
        catch01 = ImageIO.read(new File(
                "fish/images/"+name+"_catch_01.png"));
        catch02 = ImageIO.read(new File(
                "fish/images/"+name+"_catch_02.png"));
    }
    @Override//系统会自动调用
    public void run(){
        while(true){
            try {
                if(catched){//鱼被抓到
                    //翻腾几下
                    turnOver();
                    //将鱼标记为没被抓状态
                    catched = false;
                    //鱼消失
                    getOut();
                }else{//鱼没被抓到
                    move();
                    Thread.sleep(50);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //鱼消失的方法
    private void getOut() {
        x = 800;
        Random r = new Random();
        y = r.nextInt(480-height);
        speed = r.nextInt(5)+1;
    }

    //鱼潘腾的方法
    private void turnOver() {
        try {
            for(int i=0;i<5;i++){
                image = catch01;
                Thread.sleep(100);
                image = catch02;
                Thread.sleep(100);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    /**鱼游动的方法*/
    public void move(){
        image = images[index++%10];
        x-=speed;
        if(x<-width){
            x=800;
            Random r = new Random();
            y = r.nextInt(480-height);
            speed = r.nextInt(5)+1;
        }
    }


}//Fish结束

//Net开始
class Net{
    BufferedImage image;//渔网图片
    int width;
    int height;
    int x;//渔网中心点坐标
    int y;
    boolean show;//是否显示 true显示

    public Net() throws IOException {
        image = ImageIO.read(
                new File("fish/images/net09.png"));
        width = image.getWidth();
        height = image.getHeight();
        Random r = new Random();
        x = r.nextInt(800-width);
        y = r.nextInt(480-height);
        show = true;
    }



    /**捕鱼的方法*/
    public boolean catchThe(Fish fish) {
        int dx = this.x-fish.x;
        int dy = this.y-fish.y;
        return dx>0 && dx<fish.width
                && dy>0 && dy<fish.height;
    }
    /**移动的方法*/
    public void moveTo(int x, int y) {
        this.x = x;
        this.y = y;
    }



}//Net结束








